﻿//IT eBooks API Info Library. Simple .Net library to access the IT eBooks API Info http://it-ebooks-api.info/
//Copyright (C) 2014 Italo Pessoa <italo.pessoa@hotmail.com>

//This file is part of the program IT eBooks API Info Library.

//IT eBooks API Info Library is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITeBooks.API.Library.Model
{
    /// <summary>
    /// Class to represent the query result
    /// </summary>
    public class QueryResult
    {
        #region Private Members

        private string _error;
        private double _time;
        private int _page;
        private int _total;
        private EBook[] _eBooks;
        private EBook _eBook;

        #endregion

        #region Constructor

        /// <summary>
        /// Create a new Result
        /// </summary>
        /// <param name="error">Error</param>
        public QueryResult(string error)
        {
            this._error = error;
        }

        /// <summary>
        /// Create a new Result
        /// </summary>
        /// <param name="error">Error message</param>
        /// <param name="time">Time to search</param>
        /// <param name="page">Number of pages</param>
        /// <param name="total">Number of results</param>
        /// <param name="eBooks">eBooks</param>
        public QueryResult(string error, double time, int page, int total, EBook[] eBooks)
        {
            this._error = error;
            this._time = time;
            this._page = page;
            this._total = total;
            this._eBooks = eBooks;
        }

        /// <summary>
        /// Create a new Result
        /// </summary>
        /// <param name="error">Error message</param>
        /// <param name="time">Time to search</param>
        /// <param name="eBook">eBook</param>
        public QueryResult(string error, double time, EBook eBook)
        {
            this._error = error;
            this._time = time;
            this._eBook = eBook;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Error message
        /// </summary>
        public string Error
        {
            get { return this._error; }
        }

        /// <summary>
        /// Time to search
        /// </summary>
        public double Time
        {
            get { return this._time; }
        }

        /// <summary>
        /// Number of pages
        /// </summary>
        public int Page
        {
            get { return this._page; }
        }

        /// <summary>
        /// Number of results
        /// </summary>
        public int Total
        {
            get { return this._total; }
        }

        /// <summary>
        /// Ebooks
        /// </summary>
        public EBook[] EBooks
        {
            get { return this._eBooks; }
        }
        
        /// <summary>
        /// Ebook
        /// </summary>
        public EBook EBook
        {
            get { return this._eBook; }
        }

        #endregion

        #region Equals/GetHashCode

        public override bool Equals(object obj)
        {
            QueryResult aux = obj as QueryResult;


            if (!this._error.Equals(aux.Error))
                return false;
            if (this._time != aux.Time)
                return false;
            if (this._page != aux.Page)
                return false;
            if (this._total != aux.Total)
                return false;

            if ((this.EBook != null) && (aux.EBook == null))
                return false;
            if ((this.EBook == null) && (aux.EBook != null))
                return false;

            if ((this.EBooks != null) && (aux.EBooks == null))
                return false;
            if ((this.EBooks == null) && (aux.EBooks != null))
                return false;

            if ((this.EBooks != null) && (aux.EBooks != null))
            {
                if (this.EBooks.Length != aux.EBooks.Length)
                    return false;

                for (int i = 0; i < this.EBooks.Length; i++)
                {
                    if (!this.EBooks[i].Equals(aux.EBooks[i]))
                        return false;
                }
            }

            if (this.EBook != null && aux.EBook != null)
                return this._eBook.Equals(aux.EBook);


            return true;
        }

        #endregion
    }
}
